package method

const Header = `
package dal

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_frontend_api/biz/config"
	"mall_frontend_api/biz/model"
)
`

const ServiceHeader = `
type {{.TableName}}DAO struct {}

type {{.TableName}}ListParams struct {
	Condition []gen.Condition
	Page      int
	Size      int
	Sort      []field.Expr
}

func new{{.TableName}}DAO() *{{.TableName}}DAO {
	return &{{.TableName}}DAO{}
}
`

const MGet = `
func (c *{{.TableName}}DAO) MGet(ctx context.Context, ids []int64) (map[int64]*model.{{.TableName}}, error) {
	list, err := {{.TableName}}.WithContext(ctx).Where(
		{{.TableName}}.BizID.Eq(config.Values.BizID),
		{{.TableName}}.ID.In(ids...),
		{{.TableName}}.DeletedAt.IsNull(),
	).Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.MGet{{.TableName}} err: %v", err)
		return nil, err
	}
	var data = make(map[int64]*model.{{.TableName}})
	for _, info := range list {
		data[info.ID] = info
	}
	return data, nil
}
`

const GetByConditions = `
func (c *{{.TableName}}DAO) GetByConditions(ctx context.Context, condition []gen.Condition) (*model.{{.TableName}}, error) {
	tx := {{.TableName}}.WithContext(ctx).Where(
		{{.TableName}}.BizID.Eq(config.Values.BizID),
		{{.TableName}}.DeletedAt.IsNull(),
	)
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	info, err := tx.First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.Get{{.TableName}}ByConditions failed, %s", err.Error())
		return nil, err
	}
	return info, nil
}
`

const GetListWithTotal = `
func (c *{{.TableName}}DAO) GetListWithTotal(ctx context.Context, params *{{.TableName}}ListParams) ([]*model.{{.TableName}}, int64, error) {
    tx := {{.TableName}}.WithContext(ctx).Where({{.TableName}}.BizID.Eq(config.Values.BizID), {{.TableName}}.DeletedAt.IsNull())
	if len(params.Condition) > 0 {
		tx = tx.Where(params.Condition...)
	}
	if len(params.Sort) > 0 {
		tx = tx.Order(params.Sort...)
	}
	list, total, err := tx.FindByPage((params.Page-1)*params.Size, params.Size)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.{{.TableName}}.GetListWithTotal err: %v", err)
		return nil, 0, err
	}
	return list, total, nil
}
`

const GetList = `
func (c *{{.TableName}}DAO) GetList(ctx context.Context, condition []gen.Condition, page, size int) ([]*model.{{.TableName}}, error) {
	tx := {{.TableName}}.WithContext(ctx).Where({{.TableName}}.BizID.Eq(config.Values.BizID), {{.TableName}}.DeletedAt.IsNull())
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	if page > 0 && size > 0 {
		tx = tx.Offset((page - 1) * size).Limit(size)
	}
	list, err := tx.Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.Get{{.TableName}}List err: %v", err)
		return nil, err
	}
	return list, nil
}
`

const GetByID = `
func (c *{{.TableName}}DAO) GetInfoByID(ctx context.Context, id int64) (*model.{{.TableName}}, error) {
	info, err := {{.TableName}}.WithContext(ctx).Where({{.TableName}}.BizID.Eq(config.Values.BizID), {{.TableName}}.DeletedAt.IsNull()).Where({{.TableName}}.ID.Eq(id)).First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.Get{{.TableName}}InfoByID error: %v", err)
		return nil, err
	}
	return info, nil
}
`

const GetTotal = `
func (c *{{.TableName}}DAO) GetTotal(ctx context.Context, condition []gen.Condition) (int64, error) {
	total, err := {{.TableName}}.WithContext(ctx).Where({{.TableName}}.BizID.Eq(config.Values.BizID), {{.TableName}}.DeletedAt.IsNull()).Where(condition...).Count()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.Get{{.TableName}}Total error: %v", err)
		return 0, err
	}
	return total, nil
}
`

const Create = `
func (c *{{.TableName}}DAO) Create(ctx context.Context, data *model.{{.TableName}}) (*model.{{.TableName}}, error) {
	if err := {{.TableName}}.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.Create{{.TableName}} error: %v", err)
		return nil, err
	}
	return data, nil
}
`
const CreateWithTx = `
func (c *{{.TableName}}DAO) CreateWithTx(ctx context.Context, tx *Query, data *model.{{.TableName}}) (*model.{{.TableName}}, error) {
	if err := tx.{{.TableName}}.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.Create{{.TableName}} error: %v", err)
		return nil, err
	}
	return data, nil
}
`
const Update = `
func (c *{{.TableName}}DAO) Update(ctx context.Context, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := {{.TableName}}.WithContext(ctx).Where({{.TableName}}.BizID.Eq(config.Values.BizID)).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.Update{{.TableName}} error: %v", err)
		return err
	}
	return nil
}
`

const UpdateWithTx = `
func (c *{{.TableName}}DAO) UpdateWithTx(ctx context.Context, tx *Query, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := tx.{{.TableName}}.WithContext(ctx).Where({{.TableName}}.BizID.Eq(config.Values.BizID)).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.Update{{.TableName}} error: %v", err)
		return err
	}
	return nil
}
`

const Delete = `
func (c *{{.TableName}}DAO) Delete(ctx context.Context, condition []gen.Condition) error {
	_, err := {{.TableName}}.WithContext(ctx).Where({{.TableName}}.BizID.Eq(config.Values.BizID)).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.Delete{{.TableName}} error: %v", err)
		return err
	}
	return nil
}
`

const BatchCreate = `
func (c *{{.TableName}}DAO) BatchCreate(ctx context.Context, data []*model.{{.TableName}}) error {
	if err := {{.TableName}}.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateInBatches{{.TableName}} error: %v", err)
		return err
	}
	return nil
}
`

const BatchCreateWithTx = `
func (c *{{.TableName}}DAO) BatchCreateWithTx(ctx context.Context, tx *Query, data []*model.{{.TableName}}) ([]*model.{{.TableName}}, error) {
	if err := tx.{{.TableName}}.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.BatchCreateWithTx{{.TableName}} error: %v", err)
		return nil, err
	}
	return data, nil
}
`

const DeleteWithTx = `
func (c *{{.TableName}}DAO) DeleteWithTx(ctx context.Context, tx *Query, condition []gen.Condition) error {
	_, err := tx.{{.TableName}}.WithContext(ctx).Where({{.TableName}}.BizID.Eq(config.Values.BizID)).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.Delete{{.TableName}}WithTx error: %v", err)
		return err
	}
	return nil
}
`
